package com.motion.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.motion.dto.AdminCourseDto;
import com.motion.dto.BannerDto;
import com.motion.dto.CourseClassDto;
import com.motion.dto.CourseClasstotalDto;
import com.motion.generator.entity.Course;
import com.motion.generator.entity.CourseClass;
import com.motion.generator.mapper.CourseClassMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class CourseClassService {
    @Autowired(required = false)
    private CourseClassMapper courseClassMapper;

    public CourseClasstotalDto CourseClass(){
        QueryWrapper<CourseClass> queryWrapper=new QueryWrapper<>();
        //返回的分类条件结果
        CourseClasstotalDto answer=new CourseClasstotalDto();
        //只要couseClass中还有分类未提取，就重复提取一个CourseClasstotalDto中的CourseClassDto
        CourseClass one;
        String str;
        List<String> seand = new ArrayList<String>();
        CourseClassDto courseClassDto;
        List<CourseClassDto> courseClass=new ArrayList<CourseClassDto>();
        int number=0;
        //每次循环获取一个CouseClassDto
        while (courseClassMapper.selectCount(queryWrapper)!=0){
            //清空CourseClassDto,其他
            courseClassDto=null;
            seand.clear();
            courseClass=null;
            //获取随机一个couseClass的name
            one=courseClassMapper.selectOne(queryWrapper);
            str=one.getClassName();
            courseClassDto.setName(str);
            //获取所有该name的list
            courseClassDto.setTab(courseClassMapper.selectList(new QueryWrapper<CourseClass>().eq("className",str)));
            courseClass.add(courseClassDto);
            //清除条件选择器清除该name的coursclass
            queryWrapper.ne("className",str);
            number=number+1;
        }
        answer.setNumber(number);
        answer.setCourseClass(courseClass);
        return answer;
    }

    //1.根据classname，找到classValule
    public List<List<CourseClass>> nameToValue(List<String> str){
        List<List<CourseClass>> result=new ArrayList<List<CourseClass>>();
        QueryWrapper<CourseClass> queryWrapper=new QueryWrapper<CourseClass>();
        List<CourseClass> courseClasses;
        for (String item:str){
            queryWrapper.clear();
            courseClasses=courseClassMapper.selectList(queryWrapper.eq("Classname",item));
            result.add(courseClasses);
        }
        return result;
    }


//    public List<CourseClassDto> courseClass(){
//        List<CourseClass> courseClasses = courseClassMapper.selectList(new QueryWrapper<>());
//        HashMap<String, CourseClassDto> hashMap = new HashMap<>();
//        int num=1;
//        for(CourseClass item : courseClasses){
//            String name = item.getClassName();
//            if (!hashMap.containsKey(name)){
//                CourseClassDto courseClassDto = new CourseClassDto();
//                courseClassDto.setId(num++);
//                courseClassDto.setName(name);
//                courseClassDto.setTab(new ArrayList<CourseClass>());
//                hashMap.put(name, courseClassDto);
//            }
//            CourseClassDto courseClassDto = hashMap.get(name);
//            List<CourseClass> courseClasses1 = courseClassDto.getTab();
//            courseClasses1.add(item);
//            courseClassDto.setTab(courseClasses1);
//            hashMap.put(name, courseClassDto);
//        }
//
//        List<CourseClassDto> result = new LinkedList<>();
//        for(String key: hashMap.keySet()){
//            result.add(hashMap.get(key));
//        }
//        return result;
//    }

    public List<CourseClass> AllcourseClasses(){

        return courseClassMapper.selectList(new QueryWrapper<>());
    }

    public List<CourseClass> courseClassesByStr(LocalDateTime starTime, LocalDateTime endTime, String searchStr){
        QueryWrapper<CourseClass> queryWrapper=new QueryWrapper<>();
        if(starTime!=null)
            queryWrapper.ge("createTime",starTime);
        if (endTime!=null)
            queryWrapper.le("createTime",endTime);
        if (!searchStr.equals(""))
            queryWrapper.like("classValue",searchStr);
        return courseClassMapper.selectList(queryWrapper);
    }

    public List<CourseClass> str2CourseClassList(String str){
        QueryWrapper<CourseClass> queryWrapper=new QueryWrapper<CourseClass>();
        return courseClassMapper.selectList(queryWrapper.eq("Classname",str));
    }

    public List<CourseClass> getCourseClass(){
        QueryWrapper<CourseClass> queryWrapper=new QueryWrapper<CourseClass>();
        return courseClassMapper.selectList(queryWrapper);
    }
    public List<String> idsHashNames(String[] idList, HashMap<Long, String> hashMap){
        List<String> result = new ArrayList<>();
        for(String id:idList){
            result.add(hashMap.get(Long.valueOf(id)));
        }
        return result;

    }
    public List<CourseClass> id2CourseClass(List<Long> longList){
        QueryWrapper<CourseClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("courseClassId",longList);
        courseClassMapper.selectList(queryWrapper);
        return courseClassMapper.selectList(queryWrapper);
    }
    public IPage<CourseClass> getPageAll(Long page, Long size){
        QueryWrapper<CourseClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.ne("courseClassId",0);
        return courseClassMapper.selectPage(new Page<CourseClass>(page,size),queryWrapper);
    }

    public List<BannerDto> getBannerList(List<CourseClass> courseClassList,List<String> nameList,List<String> urlList) {
        List<BannerDto> bannerDtoList=new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        BannerDto bannerDto=new BannerDto();
        for (int t=0;t<courseClassList.size();t++) {
            bannerDto = new BannerDto();
            bannerDto.setTagId(courseClassList.get(t).getCourseClassId());
            bannerDto.setBannerName(nameList.get(t));
            bannerDto.setImgUrl(urlList.get(t));
            queryWrapper = new QueryWrapper();
            queryWrapper.eq("className", courseClassList.get(t).getClassName());
            queryWrapper.eq("classValue", "全部");
            CourseClass courseClass = courseClassMapper.selectOne(queryWrapper);
            bannerDto.setSortId(courseClass.getCourseClassId());
            if (courseClassList.get(t).getClassValue().equals("全部")) {
                bannerDto.setTagId(-1l);
            }
            bannerDtoList.add(bannerDto);
        }
        return bannerDtoList;
    }
//    public List<BannerDto> getCourseClassDto(List<CourseClass> courseClassList,List<String> nameList,List<String> urlList){
//        List<BannerDto> bannerDtoList=new ArrayList<>();
//        QueryWrapper queryWrapper = new QueryWrapper();
//        BannerDto bannerDto=new BannerDto();
//        for (int t=0;t<courseClassList.size();t++) {
//            bannerDto=new BannerDto();
//            BeanUtils.copyProperties(courseClassList.get(t),bannerDto);
////            bannerDto.setClassName(nameList.get(t));
//            bannerDto.setClassValue(nameList.get(t));
//            bannerDto.setImgUrl(urlList.get(t));
//            if (bannerDto.getClassValue().equals("全部")){
//                bannerDto.setSortId(bannerDto.getCourseClassId());
//                bannerDto.setCourseClassId(-1L);
//            }else {
//                queryWrapper = new QueryWrapper();
//                queryWrapper.eq("className", courseClassList.get(t).getClassName());
//                queryWrapper.eq("classValue", "全部");
//                CourseClass courseClass = courseClassMapper.selectOne(queryWrapper);
//                bannerDto.setSortId(courseClass.getCourseClassId());
//            }
//            bannerDtoList.add(bannerDto);
//        }
//        return bannerDtoList;
//    }
    public int ifexist(CourseClass courseClass){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("className",courseClass.getClassName());
        if(courseClassMapper.selectCount(queryWrapper)>0){
            queryWrapper.eq("classValue",courseClass.getClassValue());
            if (courseClassMapper.selectCount(queryWrapper)>0){
                //表示该分类已存在
                return 2;
            }else{
                //表示该分类组存在，分类不存在
                return 0;
            }
        }else {
            //表示该分类组不存在
            return 1;
        }
    }
    //增
    public CourseClass insertCourseClass(CourseClass courseClass){
        courseClassMapper.insert(courseClass);
        return courseClass;
    }
    //删
    public boolean deleteCourseClass(Long courseClassId){
        CourseClass courseClass=courseClassMapper.selectById(courseClassId);
        if (courseClass!=null){
            if (courseClass.getClassValue().equals("全部")){
                Map<String,Object> columnMap = new HashMap<>();
                columnMap.put("className",courseClass.getClassName());
                courseClassMapper.deleteByMap(columnMap);
                return true;
            }else {
                courseClassMapper.deleteById(courseClassId);
                return true;
            }
        }

        return false;
    }

    //改
    public CourseClass updataCourseClass(CourseClass courseClass){
        courseClassMapper.updateById(courseClass);
        return courseClass;
    }
}
